package com.sunyur.tailor.channel.web.outer.protocol.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.enums.MiddleResultEnum;
import com.sunyur.tailor.channel.service.inner.protocol.param.CreateProtocolParam;
import com.sunyur.tailor.channel.service.inner.protocol.service.SunyurProtocolService;
import com.sunyur.tailor.channel.web.outer.protocol.dto.CreateProtocolDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @Author: rongniu
 * @Description: 协议控制层
 * @Date: 2021/7/15 9:45
 * @Param:
 * @return:
 **/
@Slf4j
@RestController
@RequestMapping("/outer/protocol")
public class ProtocolController {
    @Autowired
    private SunyurProtocolService sunyurProtocolService;

    /**
     * @Author: rongniu
     * @Description: 创建协议
     * @Date: 2021/7/15 16:18
     * @Param: [createProtocolDTO]
     * @return: com.sunyur.tailor.channel.common.middle.base.ChannelResult
     **/
    @PostMapping(value = "/create", produces = MediaType.APPLICATION_JSON_VALUE)
    public ChannelResult create(@RequestBody @Valid CreateProtocolDTO createProtocolDTO) {
        log.info("ProtocolController create param:{}", JSON.toJSONString(createProtocolDTO));
        ChannelResult result = new ChannelResult<>();
        try {
            CreateProtocolParam createProtocolParam = BeanUtil.toBean(createProtocolDTO, CreateProtocolParam.class);
            result = sunyurProtocolService.createProtocol(createProtocolParam);
        } catch (Exception e) {
            log.error("ProtocolController create exception:{}", e);
            result.setError(MiddleResultEnum.PARAM_NULL.getCode(), MiddleResultEnum.PARAM_NULL.getValue());
        }
        log.info("ProtocolController create return:{}", JSON.toJSONString(result));
        return result;
    }

}